import { login } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'

const state = {
  token: getToken()
}

const mutations = {
  setToken(state, token) {
    // 将token存到vuex中
    state.token = token
    // 将token存到本地存储
    setToken(token)
  },
  // TODO --- 退出功能
  removeToken(state) {
    state.token = null
    removeToken()
  }
}

const actions = {
  async login(context, data) {
    // 调用接口
    const result = await login(data)
    console.log(result)
    context.commit('setToken', result)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
